package com.glory.wxmemsicpda.activity

import android.annotation.SuppressLint
import android.os.SystemClock
import android.view.View
import com.glory.wxmemsicpda.R
import com.glory.wxmemsicpda.base.Constant
import com.glory.wxmemsicpda.utils.MyUtils.isFastClick
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_Utils
import kotlinx.android.synthetic.main.activity_check_table.*
import kotlinx.android.synthetic.main.activity_setting.*
import kotlinx.android.synthetic.main.activity_setting.llMain
import kotlinx.android.synthetic.main.activity_setting.rlFileIP
import kotlinx.android.synthetic.main.activity_setting.rlIP
import kotlinx.android.synthetic.main.activity_setting.rlTimeOut
import kotlinx.android.synthetic.main.activity_setting.tvFileIP
import kotlinx.android.synthetic.main.activity_setting.tvIP
import kotlinx.android.synthetic.main.activity_setting.tvTimeOut
import kotlinx.android.synthetic.main.activity_setting.tvVersion

class SettingActivity : FreeUI_BaseActivity<Any?>(),View.OnClickListener{
    private val COUNTS = 5 // 点击次数
    private var mHits = LongArray(COUNTS) //记录点击次数
    private val DURATION: Long = 2000 //有效时间
    private var freeuiGeneralfragmentdialog: FreeUI_GeneralFragmentDialog? = null
    override fun getLayoutResId(): Int {
        return R.layout.activity_setting
    }

    override fun initView() {
        freeUI_titleView.setTitle("设置")
    }

    @SuppressLint("SetTextI18n")
    override fun initData() {
        tvVersion.text = FreeApi_Utils.getVerName()
        tvIP.text = Constant.REQUEST_URL
        tvMesIP.text = Constant.MES_REQUST_URL
        tvFileIP.text = Constant.FILE_URL
        tvTimeOut.text = Constant.DEFAULT_TIME_OUT + "s"
        rlIP.setOnClickListener(this)
        rlMesIP.setOnClickListener(this)
        rlFileIP.setOnClickListener(this)
    }

    override fun getBaseEntity(): FreeUI_AddViewEntity {
        return FreeUI_AddViewEntity(
            intent.getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY),
            llMain
        )
    }

    override fun onClick(view: View) {
        if (isFastClick) {
            showToast("您点击的太快了", false)
            return
        }
        when (view.id) {
            R.id.rlIP -> {
                freeuiGeneralfragmentdialog =
                    FreeApi_DialogUtils.getScanDialog(this, "请输入IP地址", Constant.REQUEST_URL) { s ->
                        freeuiGeneralfragmentdialog!!.dismiss()
                        Constant.REQUEST_URL = s
                        tvIP!!.text = Constant.REQUEST_URL
                        FreeApi_SharePreferencesUtils.setSharePre(
                            "URL_FILE",
                            "URL_IP",
                            s,
                            applicationContext
                        )
                        showToast("保存成功", true)
                    }
                freeuiGeneralfragmentdialog?.show(supportFragmentManager, "URL_IP_DIALOG")
            }
            R.id.rlMesIP -> {
                freeuiGeneralfragmentdialog =
                    FreeApi_DialogUtils.getScanDialog(this, "请输入IP地址", Constant.MES_REQUST_URL) { s ->
                        freeuiGeneralfragmentdialog!!.dismiss()
                        Constant.MES_REQUST_URL = s
                        tvMesIP!!.text = Constant.MES_REQUST_URL
                        FreeApi_SharePreferencesUtils.setSharePre(
                            "URL_FILE",
                            "MES_IP",
                            s,
                            applicationContext
                        )
                        showToast("保存成功", true)
                    }
                freeuiGeneralfragmentdialog?.show(supportFragmentManager, "MES_IP_DIALOG")
            }
            R.id.rlFileIP -> {
                freeuiGeneralfragmentdialog =
                    FreeApi_DialogUtils.getScanDialog(this, "请输入IP地址", Constant.FILE_URL) { s ->
                        freeuiGeneralfragmentdialog!!.dismiss()
                        Constant.FILE_URL = s
                        tvFileIP!!.text = Constant.FILE_URL
                        FreeApi_SharePreferencesUtils.setSharePre(
                            "URL_FILE",
                            "FILE_IP",
                            s,
                            applicationContext
                        )
                        showToast("保存成功", true)
                    }
                freeuiGeneralfragmentdialog?.show(supportFragmentManager, "FILE_IP_DIALOG")
            }
            R.id.rlTimeOut -> {
                freeuiGeneralfragmentdialog = FreeApi_DialogUtils.getScanDialog(
                    this,
                    "请输入超时时间",
                    Constant.DEFAULT_TIME_OUT
                ) { s ->
                    freeuiGeneralfragmentdialog!!.dismiss()
                    Constant.DEFAULT_TIME_OUT = s
                    tvTimeOut!!.text = Constant.DEFAULT_TIME_OUT + "s"
                    FreeApi_SharePreferencesUtils.setSharePre(
                        "TIME_OUT_FILE",
                        "TIME_OUT2",
                        s,
                        applicationContext
                    )
                    showToast("保存成功，请重启APP生效", true)
                    rlTimeOut!!.visibility = View.GONE
                }
                freeuiGeneralfragmentdialog?.show(supportFragmentManager, "TIME_OUT_DIALOG")
            }
            R.id.llCaiDan -> {
                //将mHints数组内的所有元素左移一个位置
                System.arraycopy(mHits, 1, mHits, 0, mHits.size - 1)
                //获得当前系统已经启动的时间
                mHits[mHits.size - 1] = SystemClock.uptimeMillis()
                if (mHits[0] >= SystemClock.uptimeMillis() - DURATION) {
                    mHits = LongArray(COUNTS)
                    rlTimeOut!!.visibility = View.VISIBLE
                }
            }
        }
    }
}